package com.example.project3.service.email;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * @ClassName MyEmail .java
 * @Description empty
 * @Author LiuJinyu
 * @Date 2023/5/30 10:12
 * @Version 1.0
 */
@Component
public class MyEmail {
    @Value("${spring.mail.username}")
    private String emailForm;
    @Value("${email.expiration}")
    private Integer emailExpiration;

    private JavaMailSender javaMailSender;

    public void sendEmail(String email, String code) {
        SimpleMailMessage message = new SimpleMailMessage();
        message.setSubject("验证码");
        message.setFrom(this.emailForm);
        message.setTo(email);
        //邮件抄送人
//        message.setCc("");
        //邮件隐秘抄送人(可以有多个)
//        message.setBcc("");
        //邮件发送日期
//        message.setSentDate(new Date());
        //邮件正文
        message.setText("验证码为:" + code + "，" + emailExpiration + "分钟内有效");
        //发送邮件
        javaMailSender.send(message);
    }

    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSender) {
        this.javaMailSender = javaMailSender;
    }
}
